package com.shaonianyou.component;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;
import com.shaonianyou.controller.BaseController;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	BaseController baseController;
	
	@SuppressWarnings("unchecked")
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		Map<String, Object> param = Maps.newHashMap();
		String[] strArray = username.split(CustUsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_SPLIT_CHAR);
		param.put("idcard", strArray[0]);
		param.put("mobile", strArray[1]);
		param.put("code", strArray[2]);
		param.put("openid", strArray[3]);
		Map<String, Object> resultMap = baseController.doPost("/login", param);
		if (StringUtils.equals(resultMap.get("code").toString(), "0")) {
			resultMap = (Map<String, Object>) resultMap.get("data");
		} else {
			throw new UsernameNotFoundException("用户名不存在");
		}

//		SecurityUser user = new SecurityUser();
		return null;
	}
}
